<?php
//GPS定位坐标纠偏，国标、高德、百度互转


class gpsCorrect
{
    private $xpi = 3.14159265358979324 * 3000.0 / 180.0;
    private $a = 6378245.0;                                    //长半轴
    private $ee = 0.00669342162296594323;                      //偏心率平方

    /**
     * WGS84(国标)转GCj02(高德、腾讯、谷歌)
     * @param $lon
     * @param $lat
     * @return array
     */
    public function getGpsToCorren($lon, $lat){
        $wgLat = +$lat;
        $wgLon = +$lon;
        if(!$this->outOfChina($wgLat, $wgLon)) {
            $dLat = $this->transformLat($wgLon - 105.0, $wgLat - 35.0);
            $dLon = $this->transformLon($wgLon - 105.0, $wgLat - 35.0);
            $radLat = $wgLat / 180.0 * pi();
            $magic = sin($radLat);
            $magic = 1 - $this->ee * $magic * $magic;
            $sqrtMagic = sqrt($magic);
            $dLat = ($dLat * 180.0) / (($this->a * (1 - $this->ee)) / ($magic * $sqrtMagic) * pi());
            $dLon = ($dLon * 180.0) / ($this->a / $sqrtMagic * cos($radLat) * pi());
            $lnglat=[($wgLon + $dLon), ($wgLat + $dLat)];
        } else {
            $lnglat = [$wgLon, $wgLat];
        }
        return $lnglat;
    }

    /**
     * GCj02(高德)转WGS84(国标)
     * @param $lng
     * @param $lat
     * return array
     */
    public function getCorrenToGps($lng, $lat){
        $lat = +$lat;
        $lng = +$lng;
        if ($this->outOfChina($lat, $lng)) {
            return array($lng, $lat);
        } else {
            $dlat = $this->transformLat($lng - 105.0, $lat - 35.0);
            $dlng = $this->transformLon($lng - 105.0, $lat - 35.0);
            $radlat = $lat / 180.0 * pi();
            $magic = sin($radlat);
            $magic = 1 - $this->ee * $magic * $magic;
            $sqrtmagic = sqrt($magic);
            $dlat = ($dlat * 180.0) / (($this->a * (1 - $this->ee)) / ($magic * $sqrtmagic) * pi());
            $dlng = ($dlng * 180.0) / ($this->a / $sqrtmagic * cos($radlat) * pi());
            $mglat = $lat + $dlat;
            $mglng = $lng + $dlng;
            return [$lng * 2 - $mglng, $lat * 2 - $mglat];
        }

    }

    /**
     * GCj02(高德)转BD09(百度)
     * @param $lng
     * @param $lat
     * @return float[]
     */
    public function getGcjToBd($lng, $lat){
        $lat = +$lat;
        $lng = +$lng;
        $z = sqrt($lng * $lng + $lat * $lat) + 0.00002 * sin($lat * $this->xpi);
        $theta = atan2($lat, $lng) + 0.000003 * cos($lng * $this->xpi);
        $bd_lng = $z * cos($theta) + 0.0065;
        $bd_lat = $z * sin($theta) + 0.006;
        return [$bd_lng, $bd_lat];
    }

    /**
     * BD09(百度)转GCj02(高德)
     * @param $lon
     * @param $lat
     * @return float[]|int[]
     */
    public function getBdToGc($lon, $lat){
        $bd_lon = +$lon;
        $bd_lat = +$lat;
        $x = $bd_lon - 0.0065;
        $y = $bd_lat - 0.006;
        $z = sqrt($x * $x + $y * $y) - 0.00002 * sin($y * $this->xpi);
        $theta = atan2($y, $x) - 0.000003 * cos($x * $this->xpi);
        $gg_lng = $z * cos($theta);
        $gg_lat = $z * sin($theta);
        return array($gg_lng, $gg_lat);
    }

    public function transform($wgLat, $wgLon, $latlng) {
        if ($this->outOfChina($wgLat, $wgLon)) {
            $latlng[0] = $wgLat;
            $latlng[1] = $wgLon;
            return;
        }
        $dLat = $this->transformLat($wgLon - 105.0, $wgLat - 35.0);
        $dLon = $this->transformLon($wgLon - 105.0, $$wgLat - 35.0);
        $radLat = $wgLat / 180.0 * pi();
        $magic = sin($radLat);
        $magic = 1 - $this->ee * $magic * $magic;
        $sqrtMagic = sqrt($magic);
        $dLat = ($dLat * 180.0) / (($this->a * (1 - $this->ee)) / ($magic * $sqrtMagic) * pi());
        $dLon = ($dLon * 180.0) / ($this->a / $sqrtMagic * cos($radLat) * pi());
        $latlng[0] = $wgLat + $dLat;
        $latlng[1] = $wgLon + $dLon;
    }

    /**
     * 非国内经纬度
     * @param lat
     * @param lon
     * @return
     */
    public function outOfChina($lat, $lon) {
    if ($lon < 72.004 || $lon > 137.8347)
        return true;
    if ($lat < 0.8293 || $lat > 55.8271)
        return true;
    return false;
}

    private function transformLat($x, $y) {
        $ret = -100.0 + 2.0 * $x + 3.0 * $y + 0.2 * $y * $y + 0.1 * $x * $y + 0.2 * sqrt(abs($x));
        $ret += (20.0 * sin(6.0 * $x * pi()) + 20.0 * sin(2.0 * $x * pi())) * 2.0 / 3.0;
        $ret += (20.0 * sin($y * pi()) + 40.0 * sin($y / 3.0 * pi())) * 2.0 / 3.0;
        $ret += (160.0 * sin($y / 12.0 * pi()) + 320 * sin($y * pi() / 30.0)) * 2.0 / 3.0;
        return $ret;
    }

    private function transformLon($x, $y) {
        $ret = 300.0 + $x + 2.0 * $y + 0.1 * $x * $x + 0.1 * $x * $y + 0.1 * sqrt(abs($x));
        $ret += (20.0 * sin(6.0 * $x * pi()) + 20.0 * sin(2.0 * $x * pi())) * 2.0 / 3.0;
        $ret += (20.0 * sin($x * pi()) + 40.0 * sin($x / 3.0 * pi())) * 2.0 / 3.0;
        $ret += (150.0 * sin($x / 12.0 * pi()) + 300.0 * sin($x / 30.0 * pi())) * 2.0 / 3.0;
        return $ret;
    }
}